<template>
	<view class="container">
		<uni-section  type="line">
			<view class="example">
				<uni-forms ref="baseForm" :modelValue="obj" >
					
					<view class="uni-list">
						<view class="uni-list--base">
							<view class="uni-list--border-bottom">
								适用范围
							</view>
							<view >
								<picker @change="bindPickerChange(types[index].typeId)" :value="index" :range="types" range-key="typeTitle">
									<view class="uni-border"><text>{{types[index].typeTitle}}</text></view>
								</picker>
							</view>
						</view>
					</view>
				<!-- 	实际上应该是多选 -->
				
					<uni-forms-item label="满" required>
						<uni-easyinput v-model="obj.mjqqsje" placeholder="请输入满减起始金额(元)" />
					</uni-forms-item>
					<uni-forms-item label="减" required>
						<uni-easyinput v-model="obj.mjqfd" placeholder="请输入优惠金额(元)" />
					</uni-forms-item>
					
					
					<uni-forms-item label="类别" required>
						<uni-data-checkbox v-model="obj.mjqtype" :localdata="coupontype" />
					</uni-forms-item>
					
					<uni-forms-item label="商品描述">
						<uni-easyinput type="textarea" v-model="obj.yhqms" placeholder="请输入商品详细信息" />
					</uni-forms-item>
					
					<uni-forms-item label="生效时间">
						<uni-datetime-picker type="datetime" return-type="timestamp" v-model="obj.mjqqssj"/>
					</uni-forms-item>
					
					<uni-forms-item label="结束时间">
						<uni-datetime-picker type="datetime" return-type="timestamp" v-model="obj.mjqjssj"/>
					</uni-forms-item>
					<button type="primary" @click="formSubmit">提交</button>
					
				</uni-forms>
			</view>
		</uni-section>
	</view>
</template>

<script>
	import { createNamespacedHelpers } from 'vuex'
	const { mapState, mapActions, mapGetters } = createNamespacedHelpers('goods')
	import {BASE_URL} from '../../api/ajax.js'
	export default {
	    data() {
	        const currentDate = this.getDate({
	            format: true
	        })
	        return {
				obj:{
				"mjqfd": 0,
				"mjqjssj": "",
				"mjqqsje":0 ,
				"mjqqssj": "",
				"mjqtype": 0,
				"shopid" : "1001",
				"sytype": 100001,
				"yhqms": ""
				},
				judge_confrim:'',
				title: 'picker',
	            index:0,
				coupontype: [{
					text: '满减券',
					value: 0
				},{
					text: '折扣券',
					value: 1
				}],				
	        
	    }},
	    computed: {
			...mapGetters(['types']),
	        startDate() {
	            return this.getDate('start');
	        },
	        endDate() {
	            return this.getDate('end');
	        }
	    },
		
	    methods: {

	        bindPickerChange: function(id) {
			this.obj.sytype=Number(id)//basetypeId和appendtypeId应该合并成id
			console.log(this.obj.sytype)
	        },//显示改变不了 已解决

			data_test(){
				this.obj.mjqqsje = Number(this.obj.mjqqsje)
				this.obj.mjqfd = Number(this.obj.mjqfd)
				
				if (this.obj.mjqqssj>=this.obj.mjqjssj)
				{
					uni.showToast({
						title:"日期有误"
					})
				}
				else{
					if(this.obj.mjqfd&&this.obj.mjqqsje)
					{
						this.judge_confrim='right'
					}
					else{
						uni.showToast({
							title:"金额有误"
						})	
					}
				}
			},
			formSubmit:function(){
				this.data_test()//转换数据类型，并进行显示
				console.log(this.obj)
				if (this.judge_confrim=='right')
				{
					uni.request({
							// url:options.url,
							url:BASE_URL+'/newtest/shop-mjq/creatMjq',
							method: 'POST',
							data: this.obj,
							success: (res)=>{
								let pages = getCurrentPages();
								let beforePage = pages[pages.length - 2];
								beforePage.$vm.againFresh = Math.random()
								uni.navigateBack();
								console.log(res.data)
								uni.showToast({
									title:'添加成功'
								})
								uni.hideLoading()						
							},
							fail: (err)=>{
								
								uni.showToast({
									title: '请求接口失败'
								})
								reject(err)
							}
						})
				}
				else{
					
				}
				
			},
	        getDate(type) {
	            const date = new Date();
	            let year = date.getFullYear();
	            let month = date.getMonth() + 1;
	            let day = date.getDate();
	
	            if (type === 'start') {
	                year = year - 60;
	            } else if (type === 'end') {
	                year = year + 2;
	            }
	            month = month > 9 ? month : '0' + month;
	            day = day > 9 ? day : '0' + day;
	            return `${year}-${month}-${day}`;
	        }
	    },
		onLoad() {
		this.$store.dispatch('goods/get_type')
		}
		
	}
</script>

<style>

</style>
